use core::{future::Future, pin::Pin};
use alloc::boxed::Box;
use core::task::{Context, Poll};

pub mod simple_executor;
pub mod keyboard;

pub mod executor;

pub struct Task {
    //dyn表示动态分发，Future是一个trait
    future: Pin<Box<dyn Future<Output = ()>>>,
}

impl Task {
    pub fn new(future: impl Future<Output = ()> + 'static) -> Task {
        Task {
            future:Box::pin(future),
        }
    }
    fn poll(&mut self, context: &mut Context<'_>) -> Poll<()> {
        self.future.as_mut().poll(context)
    }

    fn id(&self) -> TaskId {
        use core::ops::Deref;
        /// 1、用deref方法来获取对将来分配的堆的引用
        /// 2、转化成原始指针
        /// 3、转化成usize
        /// 4、包装成TaskId
        let addr = Pin::deref(&self.future) as *const _ as *const () as usize;
        TaskId(addr)
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
struct TaskId(usize);

